package shoppingmall.manage.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import shoppingmall.manage.mapper.AdministratorMapper;
import shoppingmall.manage.pojo.AdminUser;
import shoppingmall.manage.service.AdministratorService;

import shoppingmall.manage.dto.LoginRequestParamsVO;

import java.util.List;

@Service
public class AdministratorServiceImpl implements AdministratorService {

    @Autowired
    private AdministratorMapper administratorMapper;

    /**
     * 管理员登录接口
     * @param user
     * @return
     */
    @Override
    public AdminUser login(LoginRequestParamsVO user) {
        return administratorMapper.getByUsernameAndPassword(user);
    }

    /**
     * 获取所有的管理员信息
     * @return
     */
    @Override
    public List<AdminUser> getAllAdminUser() {
        return administratorMapper.getAllAdminUserList();
    }

    /**
     * 根据管理员用户名获取当前管理员信息
     * @param username
     * @return
     */
    @Override
    public AdminUser getAdminUserInfoByUsername(String username) {
        return administratorMapper.getAdminUserInfo(username);
    }

    /**
     * 批量删除管理员，也可单个删除管理员
     * @param usernames
     * @return
     */
    @Override
    public boolean deleteAdminUsers(List<String> usernames) {
        int affectItem =  administratorMapper.deleteAdminUsers(usernames);
        if(affectItem > 0){
            return true;
        }
        return false;
    }

    /**
     * 添加管理员用户
     * @param adminUser
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveAdminUser(AdminUser adminUser) {
        try {
            //调用mapper层对应的方法进行处理
            administratorMapper.saveAdminUser(adminUser);
        } catch (Exception e) {
            //管理员新用户数据插入失败，捕获异常
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * 更新管理员用户通过id
     * @param adminUser
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateAdminUserById(AdminUser adminUser) {
        try {
            //调用mapper层对应的方法进行处理
            int affectedItem = administratorMapper.updateAdminUser(adminUser);  //返回受影响的数据条数
            if(affectedItem > 0){
                return true;
            }
        } catch (Exception e) {
            //管理员新用户数据插入失败，捕获异常
            e.printStackTrace();
            return false;
        }
        return false;
    }
}
